#include "qabstractsliderhost.h"



#include "qcommonstruct.h"
#include "qabstractproperty.h"
#include "qpropertyfactory.h"

#include <QAbstractSlider>
#include <QVariant>

using namespace SHAREDLIB;

QAbstractSliderHost::QAbstractSliderHost(QObject *object):
    QWidgetHost(object)
{
    QAbstractProperty *pro;

    pro=QPropertyFactory::createProperty("Number");
    if(pro!=NULL)
    {
        pro->setAttribute("name","value");
        pro->setAttribute("property_name",tr("Value"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Number");
    if(pro!=NULL)
    {
        pro->setAttribute("name","minimum");
        pro->setAttribute("property_name",tr("Minimum"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Number");
    if(pro!=NULL)
    {
        pro->setAttribute("name","maximum");
        pro->setAttribute("property_name",tr("Maximum"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Number");
    if(pro!=NULL)
    {
        pro->setAttribute("name","singleStep");
        pro->setAttribute("property_name",tr("SingleStep"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Enum");
    if(pro!=NULL)
    {
        pro->setAttribute("name","orientation");
        pro->setAttribute("property_name",tr("Orientation"));
        pro->setAttribute("group","Attributes");
        ComboItems items;
        tagComboItem item;

        item.m_text=tr("Horizontal");
        item.m_value=Qt::Horizontal;
        items.append(item);

        item.m_text=tr("Vertical");
        item.m_value=Qt::Vertical;
        items.append(item);

        QVariant v;
        v.setValue<ComboItems>(items);
        pro->setAttribute("items",v);
        m_object->setProperty("orientation",Qt::Horizontal);
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Bool");
    if(pro!=NULL)
    {
        pro->setAttribute("name","invertedAppearance");
        pro->setAttribute("property_name",tr("InvertedAppearance"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Script");
    if(pro!=NULL)
    {
        pro->setAttribute("name","valueChanged");
        pro->setAttribute("property_name",tr("ValueChanged"));
        pro->setAttribute("group","Events");
        m_object->setProperty("valueChanged","");
        addProperty(pro);
    }

    appendMemery("value");
    appendMemery("singleStep");

    QAbstractSlider* e=(QAbstractSlider*)m_object;
    connect(e,SIGNAL(valueChanged(int)),this,SLOT(valueChanged(int)));
}


void QAbstractSliderHost::propertyChanged()
{
    QWidgetHost::propertyChanged();
    if(m_ready)
    {
        SHAREDLIB::QAbstractProperty    *pro=(SHAREDLIB::QAbstractProperty*)sender();
        if(pro->attribute("name").toString()=="orientation")
        {
            QRect re=getPropertyValue("geometry").toRect();
            re.setRect(re.x(),re.y(),re.height(),re.width());
            setPropertyValue("geometry",re);
        }
    }
}

void QAbstractSliderHost::setValue(int value)
{
    setPropertyValue("value",value);
}

int QAbstractSliderHost::value()
{
    return getPropertyValue("value").toInt();
}

void QAbstractSliderHost::setSingleStep(int singleStep)
{
    setPropertyValue("singleStep",singleStep);
}

int QAbstractSliderHost::singleStep()
{
    return getPropertyValue("singleStep").toInt();
}

void QAbstractSliderHost::setOrientation(int orientation)
{
    setPropertyValue("orientation",orientation);
}

int QAbstractSliderHost::orientation()
{
    return getPropertyValue("orientation").toInt();
}

void QAbstractSliderHost::setInvertedAppearance(bool invertedAppearance)
{
    setPropertyValue("invertedAppearance",invertedAppearance);
}

bool QAbstractSliderHost::invertedAppearance()
{
    return getPropertyValue("invertedAppearance").toBool();
}

void QAbstractSliderHost::valueChanged(int value)
{
    QString code=getPropertyValue("valueChanged").toString();
    if(code!="")
    {
        QMap<QString,QString> param;
        param.insert("value",QString::number(value));
        emit_exec(code,param);
    }
}
